import 'package:demo/pages/markdown_editor/markdown_editor_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_htmlful/flutter_htmlful.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class MarkdownEditorWebPage extends StatefulWidget {
  const MarkdownEditorWebPage({super.key});

  @override
  State<MarkdownEditorWebPage> createState() => _MarkdownEditorWebPageState();
}

class _MarkdownEditorWebPageState extends State<MarkdownEditorWebPage> {
  InAppWebViewController? _controller;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => MarkdownEditorCubit(),
      child: BlocBuilder<MarkdownEditorCubit, MarkdownEditorState>(
        builder: (BuildContext context, MarkdownEditorState state) {
          return SafeArea(
            child: Scaffold(
              body: Div(
                style: Style(
                  height: double.infinity,
                ),
                body: [
                  Expanded(
                    child: InAppWebView(
                      initialFile: 'assets/html/markdown.html',
                      onWebViewCreated: (controller) async {
                        _controller = controller;
                      },
                    ),
                  ),
                  TextButton(
                      onPressed: () async {
                        var content = await _controller?.evaluateJavascript(source: 'obtainContent()');
                        Navigator.pop(context, content);
                      },
                      child: const Text('发送')),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
}
